//
//  MainBannerCell.m
//  WIFI
//
//  Created by macbook on 2022/8/8.
//

#import "MainBannerCell.h"
#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "MainBannerItemCell.h"

@interface MainBannerCell ()<TYCyclePagerViewDataSource,TYCyclePagerViewDelegate>

@property (nonatomic, strong) UIView *bannerView;
@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) TYPageControl *pageControl;

@end

@implementation MainBannerCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = ColorHex(@"F7F7F7");
        [self createUI];
    }
    return self;
}

#pragma mark - CreateUI
- (void)createUI {
    [self addPagerView];
    [self addPageControl];
}

- (void)addPagerView {
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
    pagerView.frame = CGRectMake(18, 15, KScreenWidth-40, ceil((KScreenWidth-40)*0.29253));
    pagerView.clipsToBounds = YES;
    pagerView.layer.cornerRadius = 6.0f;
    pagerView.isInfiniteLoop = YES;
    pagerView.autoScrollInterval = 3.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    [pagerView registerClass:[MainBannerItemCell class] forCellWithReuseIdentifier:@"cellId"];
    [self.contentView addSubview:pagerView];
    _pagerView = pagerView;
}

- (void)addPageControl {
    TYPageControl *pageControl = [[TYPageControl alloc]init];
    pageControl.frame = CGRectMake(0, _pagerView.height - 15, _pagerView.width, 15);
    pageControl.numberOfPages = _bannerList.count;
    pageControl.currentPageIndicatorSize = CGSizeMake(6, 6);
    pageControl.pageIndicatorSize = CGSizeMake(6, 6);
    pageControl.currentPageIndicatorTintColor = ColorHex(@"#FFFFFF");
    pageControl.pageIndicatorTintColor = ColorHex(@"#288031");
    [_pagerView addSubview:pageControl];
    _pageControl = pageControl;
}

- (void)loadData {
    _pageControl.numberOfPages = _bannerList.count;
    [_pagerView reloadData];
}

#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return _bannerList.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    MainBannerItemCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    cell.index = index;
    cell.imgUrl = _bannerList[index];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame), CGRectGetHeight(pageView.frame));
    layout.itemSpacing = 0;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _pageControl.currentPage = toIndex;
    //[_pageControl setCurrentPage:newIndex animate:YES];
    DLog(@"%ld ->  %ld",fromIndex,toIndex);
}

- (void)setBannerList:(NSMutableArray *)bannerList {
    _bannerList = bannerList;
    [self loadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
